package com.itqf.Controller;

import com.github.pagehelper.PageInfo;
import com.itqf.Enum.CodeEnum;
import com.itqf.Service.Exercise.ExerciseService;
import com.itqf.Util.DTO.ResultDTO;
import com.itqf.Util.myPageInfo;
import com.itqf.Util.requestHeader.RequestHeaderUtil;
import com.itqf.entity.Exercise;
import com.itqf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

//刘龙
@Controller
public class ExerciseController {
    @Resource
    private ExerciseService exerciseService;

    //展示每个科目的习题+模糊查询条件为科目名称
    //exercisesubject模糊查询科目 exerciseid 查看完答案后到原来的位置
    //测试地址
    //http://localhost:8080/huileme/a/u/todayPractice?exercisesubject=数学&exercisegreadid=1
    @RequestMapping(value = "huileme/a/u/ExerciseController/todayPractice",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<PageInfo> findExercise(@RequestParam(defaultValue = "1") int pageindex, String exercisesubject, @RequestParam(defaultValue = "-1") int exerciseid, int exercisegreadid) {


        PageInfo pi = exerciseService.findExercise(pageindex,myPageInfo.PAGESIZE , exercisesubject, exerciseid,exercisegreadid);

      // return "show";
        //用map传值
        ResultDTO resultDTO=new ResultDTO();
        if (!pi.getList().isEmpty())
        {
            resultDTO.setData(pi);
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        }else
        {
            resultDTO.setData(pi);
            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
            resultDTO.setCode(CodeEnum.selecterror.getCode());
        }

        return resultDTO;

    }

    //查看答案
    //测试地址
    ///huileme/a/u/scanAnswer?exerciseid=${u.exerciseid}"
    @RequestMapping(value = "huileme/a/u/ExerciseController/scanAnswer",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<Exercise> findAnswer(int exerciseid) {
    //public String findAnswer(int exerciseid,ModelMap map) {
        Exercise exercise = exerciseService.findAnswer(exerciseid);
       // map.addAttribute("exercise", exercise);
       // return "answer";
        ResultDTO resultDTO=new ResultDTO();
        if (exercise!=null)
        {
            resultDTO.setData(exercise);
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
        }else {
            resultDTO.setCode(CodeEnum.selecterror.getCode());
            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
        }

        return resultDTO;
    }

    //收藏
    //状态码改成1.跳回原题
    //测试地址
    ///huileme/a/u/shoucang?exerciseid=${u.exerciseid}"
    //收藏完成后，跳回原页面
    //没有返回值
    @RequestMapping(value = "huileme/a/u/ExerciseController/shoucang",method = RequestMethod.GET)
    public ResultDTO saveExercise(Integer exerciseid, HttpServletResponse response,HttpServletRequest request) {
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();
        int i = exerciseService.saveExercise(exerciseid);
        ResultDTO resultDTO = new ResultDTO();
        //向页面发送收藏成功
        //输出js代码
        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            resultDTO.setMessage("收藏成功");
            resultDTO.setData(200);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultDTO;
    }
//我的收藏
    //展示自己收藏过的习题，没有进行分类
    //测试路径/huileme/a/u/mycollection
    @RequestMapping(value = "huileme/a/u/ExerciseController/mycollection",method = RequestMethod.GET)
    @ResponseBody
   public ResultDTO<PageInfo> mycollection(@RequestParam(defaultValue = "1") int pageindex,@RequestParam(defaultValue = "-1") int exercisegreadid,HttpServletRequest request) {
    //public String mycollection(@RequestParam(defaultValue = "1") int pageindex, ModelMap map,@RequestParam(defaultValue = "-1") int exerciseid) {
        PageInfo<Exercise> pi = exerciseService.mycollection(pageindex,1,exercisegreadid);
       // map.addAttribute("pi", pi);
        //return "collection";

        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();

        ResultDTO resultDTO=new ResultDTO();
        if (!pi.getList().isEmpty())
        {
            resultDTO.setData(pi);
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
        }else
        {
            resultDTO.setData(pi);
            resultDTO.setMessage(CodeEnum.selecterror.getMsg());
            resultDTO.setCode(CodeEnum.selecterror.getCode());
        }

        return resultDTO;
    }
    //查看收藏答案
    //测试地址
    ///huileme/a/u/collactionanswer?exerciseid=${u.exerciseid}
    @RequestMapping(value = "huileme/a/u/ExerciseController/collactionanswer",method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO<Exercise> collactionanswer(int exerciseid, HttpServletRequest request) {

        //        调用获取请求头中的工具类
        RequestHeaderUtil requestHeaderUtil=new RequestHeaderUtil();
        final User getheader = requestHeaderUtil.getheader(request);
        final String userid1 = getheader.getUserid();
   //public String collactionanswer(int exerciseid, ModelMap map) {
        Exercise exercise = exerciseService.findAnswer(exerciseid);
       // map.addAttribute("exercise", exercise);
        //return "collectanswer";

        ResultDTO resultDTO=new ResultDTO();
        if (exercise!=null)
        {
            resultDTO.setData(exercise);
            resultDTO.setCode(CodeEnum.selectsuccess.getCode());
            resultDTO.setMessage(CodeEnum.selectsuccess.getMsg());
        }
        return resultDTO;
    }
}


